package org.dreamwork.tools.sdq.conf;

import org.dreamwork.config.PropertyConfiguration;

import java.util.Properties;

public class ServerConfig extends PropertyConfiguration {
    public static final String KEY_UDP_PORT              = "dreamwork.dsq.udp.port";
    public static final String KEY_HEALTH_CHECK_INTERVAL = "dreamwork.dsq.health-check-interval";
    public static final String KEY_HEALTH_CHECK_TIMEOUT  = "dreamwork.dsq.health-check-timeout";
    public static final String KEY_QUEUE_CAPACITY        = "dreamwork.dsq.query.capacity";

    public ServerConfig (Properties props) {
        super (props);
    }

    public ServerConfig () {
        super (new Properties ());
    }

    public void setUdpPort (int port) {
        if (port <= 0 || port > 65535) {
            throw new IllegalArgumentException ("Invalid udp port number: " + port);
        }
        getRawProperties ().setProperty (KEY_UDP_PORT, String.valueOf (port));
    }

    public int getUdpPort () {
        return super.getInt (KEY_UDP_PORT, -1);
    }

    public void setHealthCheckInterval (long interval) {
        if (interval <= 0) {
            throw new IllegalArgumentException ("Invalid health check interval: " + interval);
        }
        getRawProperties ().setProperty (KEY_HEALTH_CHECK_INTERVAL, String.valueOf (-1));
    }

    public long getHealthCheckInterval () {
        return getLong (KEY_HEALTH_CHECK_INTERVAL, -1L);
    }

    public void setHealthCheckTimeout (long timeout) {
        if (timeout <= 0) {
            throw new IllegalArgumentException ("Invalid health check timeout: " + timeout);
        }
        getRawProperties ().setProperty (KEY_HEALTH_CHECK_TIMEOUT, String.valueOf (timeout));
    }

    public long getHealthCheckTimeout () {
        return getLong (KEY_HEALTH_CHECK_TIMEOUT, -1L);
    }

    public void setQueueCapacity (int capacity) {
        if (capacity <= 0) {
            throw new IllegalArgumentException ("Invalid queue capacity: " + capacity);
        }
        getRawProperties ().setProperty (KEY_QUEUE_CAPACITY, String.valueOf (capacity));
    }

    public int getQueueCapacity () {
        return getInt (KEY_QUEUE_CAPACITY, -1);
    }
}